﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using NetSysData;

namespace BladeTecWebApplication
{
	public partial class Quiz : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				NetSysData.NetSysData data;
				if (Session["AttemptID"] == null)
				{
					pnlNoQuiz.Visible = true;
					pnlQuiz.Visible = false;
				}
				else
				{
					data = new NetSysData.NetSysData();
					int nextQuestionId = data.GetAttemptNextQuestion((int)Session["AttemptID"]);
					if (nextQuestionId != -1)
					{
						LoadQuestion(nextQuestionId);
					}
					else
					{
						pnlQuiz.Visible = false;
						pnlQuizDone.Visible = true;
					}
				}
			}
		}

		private void LoadQuestion(int question_id)
		{
			NetSysData.NetSysData data = new NetSysData.NetSysData();
			QUESTION q = data.GetQuestion(question_id);

			if (q != null)
			{
				lblQuestionText.Text = q.QUESTION_TEXT;
				hfQuestionType.Value = q.QUESTION_TYPE;
				hfQuestionId.Value = q.QUESTION_ID.ToString();

				if (q.QUESTION_TYPE == "S")
				{
					// For short answer, just show the answer textbox.
					pnlShortAnswer.Visible = true;
					pnlAnswers.Visible = false;
					txtShortAnswer.Text = "";
				}
				else
				{
					// For multiple choice, build a list of answers.
					pnlShortAnswer.Visible = false;
					pnlAnswers.Visible = true;

					rblAnswers.Items.Clear();
					foreach (ANSWER a in q.ANSWERs)
					{
						rblAnswers.Items.Add(new ListItem(a.ANSWER_TEXT, a.ANSWER_ID.ToString()));
					}

				}
				lblQuizName.Text = Session["QuizName"].ToString();
				lblApplicantName.Text = Session["ApplicantName"].ToString();
				int currentQuestion = data.GetAttemptQuestionsAnswered((int)(Session["AttemptID"]));
				currentQuestion++;
				lblCurrentQuestion.Text = currentQuestion.ToString();
				lblTotalQuestions.Text = data.GetQuizNumQuestions((int)Session["AttemptID"]).ToString();
			}
			else
			{
				pnlQuestionError.Visible = true;
			}

		}

		protected void btnNext_Click(object sender, EventArgs e)
		{
			NetSysData.NetSysData data = new NetSysData.NetSysData();
			string questionType = hfQuestionType.Value;
			int questionId = Int32.Parse(hfQuestionId.Value);
			int attemptId = Int32.Parse(Session["AttemptID"].ToString());
			int nextQuestionId;

			if (questionType == "M")
			{
				if (rblAnswers.SelectedValue == "")
				{
					pnlMCNoSelection.Visible = true;
					return;
				}
				else
				{
					int answerId = Int32.Parse(rblAnswers.SelectedValue);
					data.AnswerQuestionMC(attemptId, questionId, answerId);
					pnlMCNoSelection.Visible = false;
				}
			}
			else
			{
				data.AnswerQuestionSA(attemptId, questionId, txtShortAnswer.Text);
			}

			nextQuestionId = data.GetAttemptNextQuestion((int)Session["AttemptID"]);
			if (nextQuestionId != -1)
			{
				LoadQuestion(nextQuestionId);
			}
			else
			{
				pnlQuiz.Visible = false;
				pnlQuizCompleted.Visible = true;
			}

		}
	}
}